require(["common", "queryString", "coAjax", "routie", "serverNotify", "href", './js/tools/getarea.js'], function(jSouper, QueryString, coAjax, routie, serverNotify, href) {
	//订单取消的触发事件
	App.set("$Event.cancel_ach_success", function(vm, result) {
		href.jump("/main-beta.html");
	});

	function _set_user_pay_ach(pay_ach) {
		App.set("user.pay_ach", pay_ach);
	};
	routie({
		"*": function(hash) {
			var queryString = new QueryString(hash);
			var ach_id = queryString.get("ach_id");
			if (!ach_id) {
				alert("error", "无效的订单编号");
				href.jump("/");
			}
			//获取订单信息
			coAjax.get(appConfig.user.ach_by_id + ach_id, function(result) {
				_set_user_pay_ach(result.result);
				//App.set('aa', result.result);
			}, function(errorCode) {
				console.log("errorCode:", errorCode);
				alert("error", "无效的订单编号");
			});
			//支付
			coAjax.get(appConfig.user.ach_by_id + ach_id, function(result) {
				var pay = result.result;
				var price = pay.cash;
				var telpay = "http://www.sportyuan.com/tenpay/tenpay.php?order_no=" + ach_id + "&product_name=商品&order_price=" + price + "&trade_mode=1&remarkexplain=商品内容";
				App.set('telpay', telpay);
			});
			//获取可抵用的额度
			coAjax.get(appConfig.user.bus_bind_by_id, {
				bus_id: appConfig.bus_id
			}, function(result) {
				App.set("$Cache.bus_bind", result.result);
			});
		}
	});

	App.set("$Event.pays.change_pay_method", function(e) {
		var pay_method = App.get("$Cache.pays.buy_method");
		coAjax.put(appConfig.user.ach_change_pay_method + App.get("user.pay_ach._id"), {
			pay_method: pay_method
		}, function(result) {
			_set_user_pay_ach(result.result);
			alert("success", "支付方式修改成功");
		}, function(errorCode, xhr, errorMsg) {
			//数据还原
			App.getModel().touchOff("user.pay_ach.info.pay_method");
			alert("error", errorMsg);
		});
	});
	//获取用户地址列表
	coAjax.get(appConfig.user.address_detail, function(result) {
		App.set("user.address_detail", result.result);
		App.getModel().touchOff("user.pay_ach.info.user_address")
	});

	// 添加地址
	App.set("$Event.add_user_address", function() {
		var new_user_address = {
			province: $("[name='province']").val(),
			town: $("[name='city']").val(),
			county: $("[name='area']").val(),
			// street:
			detail: $("[name='detailed_addre']").val(),
			mobilenumber: $("[name='mobile_num']").val(),
			phonenumber: $("[name='phone_num']").val(),
			postcode: $("[name='zip_code']").val(),
			name: $("[name='name']").val()
		};
		coAjax.post(appConfig.user.add_address, new_user_address, function(result) {
			var address_detail = App.get("user.address_detail");
			address_detail.unshift(result.result);
			console.log(result.result);
			App.set("user.address_detail", address_detail);
			App.getModel().touchOff("user.pay_ach.info.user_address");
			alert("success", '地址添加成功')
			coAjax.put(appConfig.user.ach_change_user_address + App.get("user.pay_ach._id"), {
				user_address_id: result.result._id
			}, function(result) {
				alert("success", "收获地址修改成功");
				App.set("user.pay_ach.info.user_address", result.result);
			}, function(errorCode, xhr, errorMsg) {
				App.getModel().touchOff("user.pay_ach.info.user_address._id")
				alert("error", errorMsg);
			});
		});
	});
	//改变订单使用的收获地址
	App.set("$Event.pays.change_user_address", function(e, vm) {
		coAjax.put(appConfig.user.ach_change_user_address + App.get("user.pay_ach._id"), {
			user_address_id: vm.get("_id")
		}, function(result) {
			alert("收获地址修改成功");
			App.set("user.pay_ach.info.user_address", result.result);
		}, function(errorCode, xhr, errorMsg) {
			App.getModel().touchOff("user.pay_ach.info.user_address._id")
			alert("error", errorMsg);
		});
	});
	/*
	 * Sock连接
	 */
	var user_notify = serverNotify("user");
	user_notify.on("pay_success", function(new_ach_info) {
		_set_user_pay_ach(new_ach_info);
		href.jump("/personal-beta.html");
	});
	user_notify.on("ach.state.changed", function(ach_info) {
		if (App.get("user.pay_ach._id") == ach_info.ach_id) {
			App.set("user.pay_ach.info.state", ach_info.state);
		}
	})

	//绑定优惠码
	App.set("$Event.pay_ach.bind_coupon", function(e) {
		coAjax.put(appConfig.user.ach_bind_coupon, {
			ach_id: App.get("user.pay_ach._id"),
			coupon_id: App.get("$Cache.pay_ach.coupon_id")
		}, function(result) {
			alert("优惠码绑定成功");
			_set_user_pay_ach(result.result);
		});
	});
	//使用抵扣金额
	App.set("$Event.pay_ach.bind_acc", function(e) {
		coAjax.put(appConfig.user.ach_bind_acc, {
			ach_id: App.get("user.pay_ach._id"),
			bind_acc: App.get("$Cache.pay_ach.bind_acc")
		}, function(result) {
			alert("抵扣成功");
			_set_user_pay_ach(result.result);
		});
	});
});